package com.design.strategy;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 策略模式上下文
 *
 * @author Zee
 * @date 2020年4月30日
 * @since v1.0
 */
@Component
public class StrategyContext {
    private final ConcurrentHashMap<String, StrategyService> strategyMap = new ConcurrentHashMap<>();

    private StrategyService getStrategyService(String type) {
        return strategyMap.get(type);
    }

    public void setStrategyService(String type, StrategyService strategyService) {
        strategyMap.put(type, strategyService);
    }

    public void execute(String type) {
        StrategyService strategyService = getStrategyService(type);
        if (ObjectUtil.isNotNull(strategyService)) {
            strategyService.execute();
        }

    }
}
